
package com.joysuch.wwyt.mh.controller;

import java.util.ArrayList;
import java.util.List;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.mh.bean.*;
import com.joysuch.wwyt.mh.bean.GlobalConfigBean.LevelConfig;
import com.joysuch.wwyt.mh.entity.MhAlarmConfig;
import com.joysuch.wwyt.mh.service.MhAlarmRuleService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "监测点-报警设置")
@RequestMapping("/mh/alarm-rule")
public class MhAlarmRuleController {

    @Autowired
    private MhAlarmRuleService mhAlarmRuleService;

    @GetMapping("/list")
    public ResultBean list(String name, String defaultConfig, Long monitoringTypeId, Integer page, Integer size) {
        Page<MhAlarmRuleListBean> list = mhAlarmRuleService.list(name, defaultConfig, monitoringTypeId,
                QPageRequest.of(page, size));
        return ResultBean.pageData(list.getContent(), list.getTotalElements());
    }

    @PostMapping("/save")
    public ResultBean add(@RequestBody MhJobBean MhJobBean) {
        System.out.println(MhJobBean);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/getById")
    public ResultBean getById(Long id, Pageable pageable) {
        MhJobQueryBean byId = mhAlarmRuleService.getById(id, pageable);
        MhJobQueryBean byId2 = mhAlarmRuleService.getById2(id, pageable);
        ArrayList<MhJobQueryBean> list = new ArrayList<>();
        list.add(byId);
        list.add(byId2);
        return ResultBean.wrapSuccessfulResult(list);
    }

    @GetMapping("/global")
    public ResultBean globalJob(Long id) {
        List<MhJobQueryBean> mhJobQueryBeans = mhAlarmRuleService.globalJob(id);
        return ResultBean.wrapSuccessfulResult(mhJobQueryBeans);
    }

    @GetMapping("/monitor-point-config")
    public ResultBean queryGlobalConfig(Long monitorPointId) {
        Assert.notNull(monitorPointId, "监测点ID不能为空");
        MonitorPointConfigBean config = mhAlarmRuleService.queryMonitorPointConfig(monitorPointId);
        return ResultBean.wrapSuccessfulResult(config);
    }

    /**
     * 全局配置 某一级别的配置
     * 
     * @param monitorPointId
     * @return
     */
    @GetMapping("/global-type-config")
    public ResultBean queryGlobalConfig(Long monitorPointId, Integer level) {
        Assert.notNull(monitorPointId, "监测点ID不能为空");
        Assert.notNull(level, "级别不能为空");
        LevelConfig config = mhAlarmRuleService.globalTypeConfig(monitorPointId, level);
        return ResultBean.wrapSuccessfulResult(config);
    }

    @GetMapping("/global-config")
    public ResultBean queryGlobalConfig() {
        GlobalConfigBean globalConfig = mhAlarmRuleService.queryGlobalConfig();
        return ResultBean.wrapSuccessfulResult(globalConfig);
    }

    @PostMapping("/update-monitor-point-config")
    public ResultBean updateMonitorPointConfig(@RequestBody MonitorPointConfigBean bean) {
        mhAlarmRuleService.updateMonitorPointConfig(bean);
        return ResultBean.wrapSuccessfulResult("");
    }

    @PostMapping("/update-global-config")
    public ResultBean updateGlobalConfig(@RequestBody GlobalConfigBean bean) {
        mhAlarmRuleService.updateGlobalConfig(bean);
        return ResultBean.wrapSuccessfulResult("");
    }

    @PostMapping("/edits")
    public ResultBean edits(@RequestBody MhAlarmRuleJobListBean mhAlarmRuleJob) {
        mhAlarmRuleService.edits(mhAlarmRuleJob);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/update-single")
    public ResultBean updateSingle(@RequestBody MhAlarmRuleJobListBean mhAlarmRuleJobListBean) {
        System.out.println("update-single  " + mhAlarmRuleJobListBean);
        String[] infoJob = mhAlarmRuleJobListBean.getInfoJob();
        String[] deartLimit = mhAlarmRuleJobListBean.getDeartLimit();
        String[] channelParserClass = mhAlarmRuleJobListBean.getChannelParserClass();
        String[] specifyUsers = mhAlarmRuleJobListBean.getSpecifyUsers();
        String[] defaultConfig = mhAlarmRuleJobListBean.getDefaultConfig();
        MhAlarmRuleConfigBean level1Config = null;
        if (infoJob[0] != null && !"".equals(infoJob[0])) {
            level1Config = parseConfig(infoJob[0], deartLimit[0], channelParserClass[0], specifyUsers[0],
                    defaultConfig[0]);
        } else {
            level1Config = parseConfig(null, deartLimit[0], channelParserClass[0], specifyUsers[0], defaultConfig[0]);
        }
        MhAlarmRuleConfigBean level2Config = null;
        if (infoJob[1] != null && !"".equals(infoJob[1])) {
            level2Config = parseConfig(infoJob[1], deartLimit[1], channelParserClass[1], specifyUsers[1],
                    defaultConfig[1]);
        } else {
            level2Config = parseConfig(null, deartLimit[1], channelParserClass[1], specifyUsers[1], defaultConfig[1]);
        }
        mhAlarmRuleService.updateSingleMonitoringPointConfig(mhAlarmRuleJobListBean.getMonitoringPointId(),
                level1Config, level2Config);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/update")
    public ResultBean update(@RequestBody MhAlarmRuleJobListBean mhAlarmRuleJobListBean) {
        System.out.println(mhAlarmRuleJobListBean);
        String[] infoJob = mhAlarmRuleJobListBean.getInfoJob();
        String[] deartLimit = mhAlarmRuleJobListBean.getDeartLimit();
        String[] channelParserClass = mhAlarmRuleJobListBean.getChannelParserClass();
        MhAlarmRuleConfigBean level1Config = null;
        if (infoJob[0] != null && !"".equals(infoJob[0])) {
            level1Config = parseConfig(infoJob[0], deartLimit[0], channelParserClass[0], null);
        }
        MhAlarmRuleConfigBean level2Config = null;
        if (infoJob[1] != null && !"".equals(infoJob[1])) {
            level2Config = parseConfig(infoJob[1], deartLimit[1], channelParserClass[1], null);
        }
        mhAlarmRuleService.saveGlobalConfig(level1Config, level2Config);
        return ResultBean.defaultSuccessResult();
    }

    private MhAlarmRuleConfigBean parseConfig(String infoJob, String departLimit, String channelParserClass,
            String specifyUsers, String defaultConfig) {
        MhAlarmRuleConfigBean bean = parseConfig(infoJob, departLimit, channelParserClass, defaultConfig);
        if (specifyUsers != null && specifyUsers.length() > 0) {
            String[] ss = specifyUsers.split(",");
            List<Long> users = new ArrayList<>(ss.length);
            for (String s : ss) {
                if (s.trim().length() > 0) {
                    users.add(Long.parseLong(s.trim()));
                }
            }
            bean.setSpecifyUsers(users);
        }
        return bean;
    }

    private MhAlarmRuleConfigBean parseConfig(String infoJob, String departLimit, String channelParserClass,
            String defaultConfig) {
        MhAlarmRuleConfigBean b = new MhAlarmRuleConfigBean();
        b.setLimitDepart("Y".equalsIgnoreCase(departLimit));
        b.setDefaultConfig("Y".equalsIgnoreCase(defaultConfig));
        String[] ss = channelParserClass.split(",");
        List<Long> channels = new ArrayList<>(ss.length);
        for (String s : ss) {
            if (s.trim().length() > 0) {
                channels.add(Long.parseLong(s.trim()));
            }
        }
        b.setInformChannels(channels);
        String[] groups = infoJob.split(";");
        List<MhAlarmRuleConfigBean.MonitoringTypeJobGroup> groupList = new ArrayList<>();
        for (String s : groups) {
            String[] split = s.split(",");
            MhAlarmRuleConfigBean.MonitoringTypeJobGroup m = new MhAlarmRuleConfigBean.MonitoringTypeJobGroup();
            m.setMonitoringTypeId(Long.parseLong(split[0]));
            if (split[1] != null && !"".equals(split[1])) {
                m.setJobId(Long.parseLong(split[1]));
            }
            groupList.add(m);
        }
        b.setInformGroups(groupList);
        return b;
    }

    @RequiresPermissions("mh:alarm-rule:saveAlarmConfig")
    @PostMapping("/saveAlarmConfig")
    public ResultBean alarmConfig(@RequestBody MhAlarmConfigBean bean) {
        MhAlarmConfig config = mhAlarmRuleService.saveAlarmConfig(bean);
        return ResultBean.success(config);
    }

    @GetMapping("/getAlarmConfigById")
    public ResultBean alarmConfig(Long id) {
        try {
            MhAlarmConfigBean bean = mhAlarmRuleService.getAlarmConfigById(id);
            return ResultBean.success(bean);
        } catch (Exception e) {
            return ResultBean.fail(101,"查询失败");
        }
    }

    @GetMapping("/getAlarmConfigList")
    public ResultBean getAlarmConfigList() {
        try {
            List<MhAlarmConfigBean> list = mhAlarmRuleService.getAlarmConfigList();
            return ResultBean.success(list);
        } catch (Exception e) {
            return ResultBean.success(200);
        }
    }
}
